package com.example.smartwaterapi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartwaterapi.entity.WaterRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 饮水记录Mapper接口
 */
@Mapper
public interface WaterRecordMapper extends BaseMapper<WaterRecord> {
    
    /**
     * 批量插入饮水记录
     * @param records 记录列表
     * @return 插入数量
     */
    int batchInsert(@Param("records") List<WaterRecord> records);
    
    /**
     * 获取用户每日饮水总量
     */
    @Select("SELECT SUM(amount) FROM water_records WHERE user_id = #{userId} AND record_date = #{date}")
    Integer getDailyTotal(@Param("userId") Long userId, @Param("date") LocalDate date);
    
    /**
     * 获取用户指定日期范围内每日饮水量
     */
    @Select("SELECT record_date, SUM(amount) as total FROM water_records " +
            "WHERE user_id = #{userId} AND record_date BETWEEN #{startDate} AND #{endDate} " +
            "GROUP BY record_date ORDER BY record_date")
    List<Map<String, Object>> getDailyTotalsByDateRange(@Param("userId") Long userId, 
                                                       @Param("startDate") LocalDate startDate, 
                                                       @Param("endDate") LocalDate endDate);
    
    /**
     * 获取用户指定日期的饮水类型分布
     */
    @Select("SELECT drink_type, SUM(amount) as total FROM water_records " +
            "WHERE user_id = #{userId} AND record_date = #{date} " +
            "GROUP BY drink_type")
    List<Map<String, Object>> getDrinkTypeDistribution(@Param("userId") Long userId, @Param("date") LocalDate date);
    
    /**
     * 获取用户指定日期的每小时饮水量
     */
    @Select("SELECT hour, SUM(amount) as total FROM water_records " +
            "WHERE user_id = #{userId} AND record_date = #{date} " +
            "GROUP BY hour ORDER BY hour")
    List<Map<String, Object>> getHourlyDistribution(@Param("userId") Long userId, @Param("date") LocalDate date);
    
    /**
     * 获取用户所有时间的饮水总量
     */
    @Select("SELECT SUM(amount) FROM water_records WHERE user_id = #{userId}")
    Object getUserAllTimeTotal(@Param("userId") Long userId);
} 